package camel.http;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.ModelCamelContext;
import org.junit.Test;

import camel.utils.CamelUtils;

public class HttpRouteApp {
	public static String res = null;

	public static void main(String[] args) throws Exception {
		ModelCamelContext camelContext = CamelUtils.getInstanCamelContext();
		camelContext.addRoutes(new RouteBuilder() {
			@Override
			public void configure() throws Exception {
				// TODO Auto-generated method stub
				from("timer://myTimer?period=20000")
						// .to("http://220.197.219.190:9080/G2/gfm/common-ce!projectce.do").to("file:d:\\tmp?fileName=http.txt");
						.to("http://220.197.219.190:9080/G2/gfm/common-ce!projectce.do").process(new Processor() {
							@Override
							public void process(Exchange exchange) throws Exception {
								// TODO Auto-generated method stub
								res = exchange.getMessage().getBody(String.class);
							}
						});
			}
		});

		camelContext.start();
		boolean loop = true;
		while (loop) {
			Thread.sleep(2000);
			loop = false;
			System.out.println("\n" + res);
			System.out.println(">>>>>>>循环");
		}
		camelContext.stop();
	}

	@Test
	public void test1() throws Exception {
		/*
		 * ModelCamelContext camelContext = CamelUtils.getInstanCamelContext();
		 * ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
		 * producerTemplate.send(
		 * "http://220.197.219.190:9080/G2/gfm/common-ce!projectce.do",new Processor() {
		 * 
		 * @Override public void process(Exchange exchange) throws Exception { // TODO
		 * Auto-generated method stub String res1 =
		 * exchange.getMessage().getBody(String.class);
		 * System.out.println(" res = "+res1); } }); camelContext.start(); boolean loop
		 * = true; while (loop) { Thread.sleep(2000); loop = false;
		 * System.out.println(">>>>>>>循环"); } camelContext.stop();
		 */

		ModelCamelContext camelContext = CamelUtils.getInstanCamelContext();
		camelContext.addRoutes(new RouteBuilder() {
			@Override
			public void configure() throws Exception {
				from("direct:start").process(new Processor() {

					@Override
					public void process(Exchange exchange) throws Exception {
						// TODO Auto-generated method stub
						String headerString = (String) exchange.getIn().getHeader("name");
						String bodyString = exchange.getIn().getBody(String.class);
						System.out.println(headerString + "|" + bodyString);
					}
				}).setHeader(Exchange.HTTP_METHOD, constant("GET"))
						.to("http://220.197.219.190:9080/G2/gfm/common-ce!projectce.do");
			}
		});
		camelContext.start();
		ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
		String result = producerTemplate.requestBodyAndHeader("direct:start", "mybody", "name", "Kermit", String.class);
		System.out.println("result = " + result);

	}
}
